package com.yuqing.sentimentapp.workspace.epidemic.personal;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.androidkun.xtablayout.XTabLayout;
import com.linkface.baselibrary.http.BaseBean;
import com.yuqing.sentimentapp.R;
import com.yuqing.sentimentapp.base.BaseActivity;
import com.yuqing.sentimentapp.base.BaseObserver;
import com.yuqing.sentimentapp.module.ModuleManager;
import com.yuqing.sentimentapp.workspace.epidemic.personal.entity.EpidemicPersonalInfoResult;
import com.yuqing.sentimentapp.workspace.epidemic.personal.entity.EpidemicPersonalReportResult;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/** 疫情防控
 * Created by shenhe on 2020/4/3.
 */
public class EpidemicPersonalFillInActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView mIvBack;
    @BindView(R.id.tabs)
    XTabLayout mTabs;
    @BindView(R.id.et_cold_commander_count)
    EditText mEtColdCommanderCount;
    @BindView(R.id.et_cold_fireman_count)
    EditText mEtColdFiremanCount;
    @BindView(R.id.et_cold_clerk_count)
    EditText mEtColdClerkCount;
    @BindView(R.id.et_cold_other_count)
    EditText mEtColdOtherCount;
    @BindView(R.id.et_cold_family_count)
    EditText mEtColdFamilyCount;
    @BindView(R.id.ll_cold)
    LinearLayout mLlCold;
    @BindView(R.id.et_fever_commander_count)
    EditText mEtFeverCommanderCount;
    @BindView(R.id.et_fever_fireman_count)
    EditText mEtFeverFiremanCount;
    @BindView(R.id.et_fever_clerk_count)
    EditText mEtFeverClerkCount;
    @BindView(R.id.et_fever_other_count)
    EditText mEtFeverOtherCount;
    @BindView(R.id.et_fever_family_count)
    EditText mEtFeverFamilyCount;
    @BindView(R.id.ll_fever)
    LinearLayout mLlFever;
    @BindView(R.id.et_suspect_commander_count)
    EditText mEtSuspectCommanderCount;
    @BindView(R.id.et_suspect_fireman_count)
    EditText mEtSuspectFiremanCount;
    @BindView(R.id.et_suspect_clerk_count)
    EditText mEtSuspectClerkCount;
    @BindView(R.id.et_suspect_other_count)
    EditText mEtSuspectOtherCount;
    @BindView(R.id.et_suspect_family_count)
    EditText mEtSuspectFamilyCount;
    @BindView(R.id.ll_suspect)
    LinearLayout mLlSuspect;
    @BindView(R.id.et_confirm_commander_count)
    EditText mEtConfirmCommanderCount;
    @BindView(R.id.et_confirm_fireman_count)
    EditText mEtConfirmFiremanCount;
    @BindView(R.id.et_confirm_clerk_count)
    EditText mEtConfirmClerkCount;
    @BindView(R.id.et_confirm_other_count)
    EditText mEtConfirmOtherCount;
    @BindView(R.id.et_confirm_family_count)
    EditText mEtConfirmFamilyCount;
    @BindView(R.id.ll_confirm)
    LinearLayout mLlConfirm;
    @BindView(R.id.et_other_stay_count)
    EditText mEtOtherStayCount;
    @BindView(R.id.et_other_touch_count)
    EditText mEtOtherTouchCount;
    @BindView(R.id.et_other_suspect_count)
    EditText mEtOtherSuspectCount;
    @BindView(R.id.et_other_seclusion_count)
    EditText mEtOtherSeclusionCount;
    @BindView(R.id.ll_other)
    LinearLayout mLlOther;
    @BindView(R.id.et_place_count)
    EditText mEtPlaceCount;
    @BindView(R.id.et_place_person_count)
    EditText mEtPlacePersonCount;
    @BindView(R.id.et_reportor)
    EditText mEtReportor;
    @BindView(R.id.et_auditor)
    EditText mEtAuditor;
    @BindView(R.id.et_phone)
    EditText mEtPhone;
    @BindView(R.id.et_remark)
    EditText mEtRemark;
    @BindView(R.id.ll_place)
    LinearLayout mLlPlace;
    @BindView(R.id.tv_save)
    TextView mTvSave;

    private boolean editEnable = true;
    private EpidemicPersonalInfoResult.DataBean actionInfo;
    private EpidemicPersonalReportResult.DataBean.SubSumBean subSum;
    /**
     * 从已上报列表跳转过来的
     */
    private EpidemicPersonalReportResult.DataBean.ReportsBean reportsInfo;

    public static void goActivity(Activity activity, EpidemicPersonalInfoResult.DataBean actionInfo, EpidemicPersonalReportResult.DataBean.SubSumBean subSum) {
        Intent intent = new Intent(activity, EpidemicPersonalFillInActivity.class);
        intent.putExtra("actionInfo", actionInfo);
        intent.putExtra("subSum", subSum);
        activity.startActivity(intent);
    }

    public static void goActivity(Activity activity, EpidemicPersonalReportResult.DataBean.ReportsBean reportsInfo) {
        Intent intent = new Intent(activity, EpidemicPersonalFillInActivity.class);
        intent.putExtra("reportsInfo", reportsInfo);
        activity.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_epidemic_personal_fill_in;
    }

    @Override
    public void initPresenter() {
        Intent intent = getIntent();
        if (intent == null) return;
        actionInfo = ((EpidemicPersonalInfoResult.DataBean) intent.getSerializableExtra("actionInfo"));
        subSum = ((EpidemicPersonalReportResult.DataBean.SubSumBean) intent.getSerializableExtra("subSum"));

        reportsInfo = ((EpidemicPersonalReportResult.DataBean.ReportsBean) intent.getSerializableExtra("reportsInfo"));
    }

    @Override
    public void initView() {
        if (actionInfo != null) {
            mEtColdCommanderCount.setText(actionInfo.getColdCommanderCount());
            mEtColdClerkCount.setText(actionInfo.getColdClerkCount());
            mEtColdFamilyCount.setText(actionInfo.getColdFamilyCount());
            mEtColdFiremanCount.setText(actionInfo.getColdFiremanCount());
            mEtColdOtherCount.setText(actionInfo.getColdOtherCount());

            mEtFeverCommanderCount.setText(actionInfo.getFeverCommanderCount());
            mEtFeverClerkCount.setText(actionInfo.getFeverClerkCount());
            mEtFeverFamilyCount.setText(actionInfo.getFeverFamilyCount());
            mEtFeverFiremanCount.setText(actionInfo.getFeverFiremanCount());
            mEtFeverOtherCount.setText(actionInfo.getFeverOtherCount());

            mEtSuspectCommanderCount.setText(actionInfo.getSuspectCommanderCount());
            mEtSuspectClerkCount.setText(actionInfo.getSuspectClerkCount());
            mEtSuspectFamilyCount.setText(actionInfo.getSuspectFamilyCount());
            mEtSuspectFiremanCount.setText(actionInfo.getSuspectFiremanCount());
            mEtSuspectOtherCount.setText(actionInfo.getSuspectOtherCount());

            mEtConfirmCommanderCount.setText(actionInfo.getConfirmCommanderCount());
            mEtConfirmClerkCount.setText(actionInfo.getConfirmClerkCount());
            mEtConfirmFamilyCount.setText(actionInfo.getConfirmFamilyCount());
            mEtConfirmFiremanCount.setText(actionInfo.getConfirmFiremanCount());
            mEtConfirmOtherCount.setText(actionInfo.getConfirmOtherCount());

            mEtOtherStayCount.setText(actionInfo.getOtherStayCount());
            mEtOtherSeclusionCount.setText(actionInfo.getOtherSeclusionCount());
            mEtOtherSuspectCount.setText(actionInfo.getOtherSuspectCount());
            mEtOtherTouchCount.setText(actionInfo.getOtherTouchCount());

            mEtPlacePersonCount.setText(actionInfo.getPlacePersonCount());
            mEtPlaceCount.setText(actionInfo.getPlaceCount());
            mEtAuditor.setText(actionInfo.getAuditor());
            mEtReportor.setText(actionInfo.getReportor());
            mEtPhone.setText(actionInfo.getPhone());
            mEtRemark.setText(actionInfo.getRemark());
        } else {
            if (subSum != null) {
                mEtColdCommanderCount.setText(subSum.getColdCommanderCount());
                mEtColdClerkCount.setText(subSum.getColdClerkCount());
                mEtColdFamilyCount.setText(subSum.getColdFamilyCount());
                mEtColdFiremanCount.setText(subSum.getColdFiremanCount());
                mEtColdOtherCount.setText(subSum.getColdOtherCount());

                mEtFeverCommanderCount.setText(subSum.getFeverCommanderCount());
                mEtFeverClerkCount.setText(subSum.getFeverClerkCount());
                mEtFeverFamilyCount.setText(subSum.getFeverFamilyCount());
                mEtFeverFiremanCount.setText(subSum.getFeverFiremanCount());
                mEtFeverOtherCount.setText(subSum.getFeverOtherCount());

                mEtSuspectCommanderCount.setText(subSum.getSuspectCommanderCount());
                mEtSuspectClerkCount.setText(subSum.getSuspectClerkCount());
                mEtSuspectFamilyCount.setText(subSum.getSuspectFamilyCount());
                mEtSuspectFiremanCount.setText(subSum.getSuspectFiremanCount());
                mEtSuspectOtherCount.setText(subSum.getSuspectOtherCount());

                mEtConfirmCommanderCount.setText(subSum.getConfirmCommanderCount());
                mEtConfirmClerkCount.setText(subSum.getConfirmClerkCount());
                mEtConfirmFamilyCount.setText(subSum.getConfirmFamilyCount());
                mEtConfirmFiremanCount.setText(subSum.getConfirmFiremanCount());
                mEtConfirmOtherCount.setText(subSum.getConfirmOtherCount());

                mEtOtherStayCount.setText(subSum.getOtherStayCount());
                mEtOtherSeclusionCount.setText(subSum.getOtherSeclusionCount());
                mEtOtherSuspectCount.setText(subSum.getOtherSuspectCount());
                mEtOtherTouchCount.setText(subSum.getOtherTouchCount());

                mEtPlaceCount.setText(subSum.getPlaceCount());
                mEtPlacePersonCount.setText(subSum.getPlacePersonCount());
            }
        }

        /**
         * 从已上报列表跳转过来的，不可编辑，隐藏保存按钮
         */
        if (reportsInfo != null) {
            mEtColdCommanderCount.setText(reportsInfo.getColdCommanderCount());
            mEtColdClerkCount.setText(reportsInfo.getColdClerkCount());
            mEtColdFamilyCount.setText(reportsInfo.getColdFamilyCount());
            mEtColdFiremanCount.setText(reportsInfo.getColdFiremanCount());
            mEtColdOtherCount.setText(reportsInfo.getColdOtherCount());

            mEtFeverCommanderCount.setText(reportsInfo.getFeverCommanderCount());
            mEtFeverClerkCount.setText(reportsInfo.getFeverClerkCount());
            mEtFeverFamilyCount.setText(reportsInfo.getFeverFamilyCount());
            mEtFeverFiremanCount.setText(reportsInfo.getFeverFiremanCount());
            mEtFeverOtherCount.setText(reportsInfo.getFeverOtherCount());

            mEtSuspectCommanderCount.setText(reportsInfo.getSuspectCommanderCount());
            mEtSuspectClerkCount.setText(reportsInfo.getSuspectClerkCount());
            mEtSuspectFamilyCount.setText(reportsInfo.getSuspectFamilyCount());
            mEtSuspectFiremanCount.setText(reportsInfo.getSuspectFiremanCount());
            mEtSuspectOtherCount.setText(reportsInfo.getSuspectOtherCount());

            mEtConfirmCommanderCount.setText(reportsInfo.getConfirmCommanderCount());
            mEtConfirmClerkCount.setText(reportsInfo.getConfirmClerkCount());
            mEtConfirmFamilyCount.setText(reportsInfo.getConfirmFamilyCount());
            mEtConfirmFiremanCount.setText(reportsInfo.getConfirmFiremanCount());
            mEtConfirmOtherCount.setText(reportsInfo.getConfirmOtherCount());

            mEtOtherStayCount.setText(reportsInfo.getOtherStayCount());
            mEtOtherSeclusionCount.setText(reportsInfo.getOtherSeclusionCount());
            mEtOtherSuspectCount.setText(reportsInfo.getOtherSuspectCount());
            mEtOtherTouchCount.setText(reportsInfo.getOtherTouchCount());

            mEtPlacePersonCount.setText(reportsInfo.getPlacePersonCount());
            mEtPlaceCount.setText(reportsInfo.getPlaceCount());
            mEtAuditor.setText(reportsInfo.getAuditor());
            mEtReportor.setText(reportsInfo.getReportor());
            mEtPhone.setText(reportsInfo.getPhone());
            mEtRemark.setText(reportsInfo.getRemark());

            mEtColdCommanderCount.setEnabled(false);
            mEtColdClerkCount.setEnabled(false);
            mEtColdFamilyCount.setEnabled(false);
            mEtColdFiremanCount.setEnabled(false);
            mEtColdOtherCount.setEnabled(false);

            mEtFeverCommanderCount.setEnabled(false);
            mEtFeverClerkCount.setEnabled(false);
            mEtFeverFamilyCount.setEnabled(false);
            mEtFeverFiremanCount.setEnabled(false);
            mEtFeverOtherCount.setEnabled(false);

            mEtSuspectCommanderCount.setEnabled(false);
            mEtSuspectClerkCount.setEnabled(false);
            mEtSuspectFamilyCount.setEnabled(false);
            mEtSuspectFiremanCount.setEnabled(false);
            mEtSuspectOtherCount.setEnabled(false);

            mEtConfirmCommanderCount.setEnabled(false);
            mEtConfirmClerkCount.setEnabled(false);
            mEtConfirmFamilyCount.setEnabled(false);
            mEtConfirmFiremanCount.setEnabled(false);
            mEtConfirmOtherCount.setEnabled(false);

            mEtOtherStayCount.setEnabled(false);
            mEtOtherSeclusionCount.setEnabled(false);
            mEtOtherSuspectCount.setEnabled(false);
            mEtOtherTouchCount.setEnabled(false);

            mEtPlacePersonCount.setEnabled(false);
            mEtPlaceCount.setEnabled(false);
            mEtAuditor.setEnabled(false);
            mEtReportor.setEnabled(false);
            mEtPhone.setEnabled(false);
            mEtRemark.setEnabled(false);

            mTvSave.setVisibility(View.GONE);
        }

        mTabs.setOnTabSelectedListener(new XTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(XTabLayout.Tab tab) {
                resetView();
                if (tab.getPosition() == 0) {
                    mLlCold.setVisibility(View.VISIBLE);
                } else if (tab.getPosition() == 1) {
                    mLlFever.setVisibility(View.VISIBLE);
                } else if (tab.getPosition() == 2) {
                    mLlSuspect.setVisibility(View.VISIBLE);
                } else if (tab.getPosition() == 3) {
                    mLlConfirm.setVisibility(View.VISIBLE);
                } else if (tab.getPosition() == 4) {
                    mLlOther.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onTabUnselected(XTabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(XTabLayout.Tab tab) {

            }
        });
    }

    private void resetView() {
        mLlCold.setVisibility(View.GONE);
        mLlFever.setVisibility(View.GONE);
        mLlConfirm.setVisibility(View.GONE);
        mLlSuspect.setVisibility(View.GONE);
        mLlOther.setVisibility(View.GONE);
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onClick(View v) {
        switch (v.getId()) {
            default:
                break;
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                if (TextUtils.isEmpty(mEtReportor.getText().toString())) {
                    showShortToast("填报人不可为空");
                    return;
                }
                if (TextUtils.isEmpty(mEtAuditor.getText().toString())) {
                    showShortToast("审核人不可为空");
                    return;
                }
                if (TextUtils.isEmpty(mEtPhone.getText().toString())) {
                    showShortToast("联系电话不可为空");
                    return;
                }
                if (actionInfo == null) {
                    actionInfo = new EpidemicPersonalInfoResult.DataBean();
                    setData();
                    saveReportInfo(actionInfo);
                } else {
                    setData();
                    updateReportInfo(actionInfo);
                }
                break;
        }
    }

    private void setData() {
        actionInfo.setColdCommanderCount(TextUtils.isEmpty(mEtColdCommanderCount.getText().toString()) ? "0" : mEtColdCommanderCount.getText().toString());
        actionInfo.setColdClerkCount(TextUtils.isEmpty(mEtColdClerkCount.getText().toString()) ? "0" : mEtColdClerkCount.getText().toString());
        actionInfo.setColdFamilyCount(TextUtils.isEmpty(mEtColdFamilyCount.getText().toString()) ? "0" : mEtColdFamilyCount.getText().toString());
        actionInfo.setColdFiremanCount(TextUtils.isEmpty(mEtColdFiremanCount.getText().toString()) ? "0" : mEtColdFiremanCount.getText().toString());
        actionInfo.setColdOtherCount(TextUtils.isEmpty(mEtColdOtherCount.getText().toString()) ? "0" : mEtColdOtherCount.getText().toString());

        actionInfo.setFeverCommanderCount(TextUtils.isEmpty(mEtFeverCommanderCount.getText().toString()) ? "0" : mEtFeverCommanderCount.getText().toString());
        actionInfo.setFeverClerkCount(TextUtils.isEmpty(mEtFeverClerkCount.getText().toString()) ? "0" : mEtFeverClerkCount.getText().toString());
        actionInfo.setFeverFamilyCount(TextUtils.isEmpty(mEtFeverFamilyCount.getText().toString()) ? "0" : mEtFeverFamilyCount.getText().toString());
        actionInfo.setFeverFiremanCount(TextUtils.isEmpty(mEtFeverFiremanCount.getText().toString()) ? "0" : mEtFeverFiremanCount.getText().toString());
        actionInfo.setFeverOtherCount(TextUtils.isEmpty(mEtFeverOtherCount.getText().toString()) ? "0" : mEtFeverOtherCount.getText().toString());

        actionInfo.setSuspectCommanderCount(TextUtils.isEmpty(mEtSuspectCommanderCount.getText().toString()) ? "0" : mEtSuspectCommanderCount.getText().toString());
        actionInfo.setSuspectClerkCount(TextUtils.isEmpty(mEtSuspectClerkCount.getText().toString()) ? "0" : mEtSuspectClerkCount.getText().toString());
        actionInfo.setSuspectFamilyCount(TextUtils.isEmpty(mEtSuspectFamilyCount.getText().toString()) ? "0" : mEtSuspectFamilyCount.getText().toString());
        actionInfo.setSuspectFiremanCount(TextUtils.isEmpty(mEtSuspectFiremanCount.getText().toString()) ? "0" : mEtSuspectFiremanCount.getText().toString());
        actionInfo.setSuspectOtherCount(TextUtils.isEmpty(mEtSuspectOtherCount.getText().toString()) ? "0" : mEtSuspectOtherCount.getText().toString());

        actionInfo.setConfirmCommanderCount(TextUtils.isEmpty(mEtConfirmCommanderCount.getText().toString()) ? "0" : mEtConfirmCommanderCount.getText().toString());
        actionInfo.setConfirmClerkCount(TextUtils.isEmpty(mEtConfirmClerkCount.getText().toString()) ? "0" : mEtConfirmClerkCount.getText().toString());
        actionInfo.setConfirmFamilyCount(TextUtils.isEmpty(mEtConfirmFamilyCount.getText().toString()) ? "0" : mEtConfirmFamilyCount.getText().toString());
        actionInfo.setConfirmFiremanCount(TextUtils.isEmpty(mEtConfirmFiremanCount.getText().toString()) ? "0" : mEtConfirmFiremanCount.getText().toString());
        actionInfo.setConfirmOtherCount(TextUtils.isEmpty(mEtConfirmOtherCount.getText().toString()) ? "0" : mEtConfirmOtherCount.getText().toString());

        actionInfo.setOtherStayCount(TextUtils.isEmpty(mEtOtherStayCount.getText().toString()) ? "0" : mEtOtherStayCount.getText().toString());
        actionInfo.setOtherSeclusionCount(TextUtils.isEmpty(mEtOtherSeclusionCount.getText().toString()) ? "0" : mEtOtherSeclusionCount.getText().toString());
        actionInfo.setOtherSuspectCount(TextUtils.isEmpty(mEtOtherSuspectCount.getText().toString()) ? "0" : mEtOtherSuspectCount.getText().toString());
        actionInfo.setOtherTouchCount(TextUtils.isEmpty(mEtOtherTouchCount.getText().toString()) ? "0" : mEtOtherTouchCount.getText().toString());

        actionInfo.setAuditor(mEtAuditor.getText().toString());
        actionInfo.setReportor(mEtReportor.getText().toString());
        actionInfo.setPhone(mEtPhone.getText().toString());
        actionInfo.setRemark(TextUtils.isEmpty(mEtRemark.getText().toString()) ? "" : mEtRemark.getText().toString());
        actionInfo.setPlacePersonCount(TextUtils.isEmpty(mEtPlacePersonCount.getText().toString()) ? "" : mEtPlacePersonCount.getText().toString());
        actionInfo.setPlaceCount(TextUtils.isEmpty(mEtPlaceCount.getText().toString()) ? "" : mEtPlaceCount.getText().toString());
    }

    private void updateReportInfo(EpidemicPersonalInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().updateEpidemicPersonalReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }

    private void saveReportInfo(EpidemicPersonalInfoResult.DataBean actionInfo) {
        ModuleManager.getInstance().saveEpidemicPersonalReportInfoResult(actionInfo)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseBean>() {
                    @Override
                    protected void hideDialog() {
                        stopProgressDialog();
                    }

                    @Override
                    protected void showDialog() {
                        startProgressDialog();
                    }

                    @Override
                    protected void onSuccess(BaseBean baseBean) {
                        showShortToast("保存成功");
                        finish();
                    }

                    @Override
                    protected void onFailure(String message) {
                        showShortToast(message);
                    }
                });
    }
}
